let request = require('co-request');
const token = require('./token');
const apiUrl = 'http://www.auewecanberra.com:4567/api/';
const rp = require('request-promise');
let co = require('co');
const Promise = require('promise');
module.exports = {
  //search 支持大多数查询，用例参考wms
  search: function  *(criteria) {
    // token.getApiToken(function (token) {
    //   console.log('here in getapitoken!')
    //   request({
    //     url: apiUrl+'inventory/?'+criteria,
    //     method: 'GET',
    //     headers: {
    //       Authorization: token
    //     }
    //   }, function (err, resp, body) {
    //     if (err) return call(err);
    //     console.log('search api:',body);
    //     return call(body);
    //   })
    // })
      var tokenn = yield *token.getApiToken();

      let result = yield request({
        uri: apiUrl+'inventory/?'+criteria,
        method: "GET",
        headers: {
          Authorization: tokenn.body
        }
      });

      return result.body;
  },

  getAllProducts: function (call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/',
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  getBrandsByCat: function (cat, call) {
    token.getApiTokenCall(function (result) {
      request({
        url: encodeURI(apiUrl+'inventory/brands/?category='+cat),
        method: 'GET',
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
    // request(apiUrl+'', function (err, resp, body) {
    //   if (err) return call(err)
    //   return call(body);
    // })
  },

  getAllCats: function (call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/categories/',
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  getAllBrands: function (call) {
    token.getApiTokenCall(function (result) {
      request({

        url: apiUrl+'inventory/brands/',
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  getProductsByBrand: function (brand, call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/?brand=lk'+encodeURI(',%'+brand+'%'),
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  getProductsByCat: function (catName, call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/?category=lk'+encodeURI(',%'+catName+'%'),
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  get_pagination: function (catName, offset, call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/?offset='+offset+'&limit=20&category=lk'+encodeURI(',%'+catName+'%'),
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  get_product_by_sn_async: function (sn) {
    return new Promise(function (resolve, reject) {
      token.getApiTokenCall(function (token) {
        request({
          url: apiUrl+'inventory/?sn=lk'+encodeURI(',%'+sn+'%'),
          headers: {
            Authorization: token
          }
        }, function (err, resp, body) {
          if (err) return reject(err);
          return resolve(JSON.parse(body));
        })
      })
    })
  },

  getProductBySn: function (sn, call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'inventory/?sn=lk'+encodeURI(',%'+sn+'%'),
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  },

  getCombos: function (call) {
    token.getApiTokenCall(function (result) {
      request({
        url: apiUrl+'sales/combo/?combo_id=0db73111f78a4c91974aebb7fd849262',
        headers: {
          Authorization: result
        }
      }, function (err, resp, body) {
        if (err) return call(err);
        return call(body);
      })
    })
  }
}
